package cn.hejiang.pet.controller;

import cn.hejiang.basic.exception.BusinessException;
import cn.hejiang.basic.util.AjaxResult;
import cn.hejiang.basic.util.LoginContext;
import cn.hejiang.basic.util.PageList;
import cn.hejiang.pet.domain.Pet;
import cn.hejiang.pet.domain.SearchMasterMsg;
import cn.hejiang.pet.query.SearchMasterMsgQuery;
import cn.hejiang.pet.service.ISearchMasterMsgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/searchMasterMsg")
public class SearchMasterMsgController {
    @Autowired
    private ISearchMasterMsgService searchMasterMsgService;


    @GetMapping
    public List<SearchMasterMsg> getAll(){
        return searchMasterMsgService.queryAll();
    }

    @PostMapping("/publish")
    public AjaxResult publish(@RequestBody SearchMasterMsg searchMasterMsg, HttpServletRequest request) {
        try {
            searchMasterMsgService.publish(searchMasterMsg, LoginContext.currentLoginInfo(request).getId());
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("系统崩溃，请下辈子在来");
        }

    }

    // 查询未处理消息
    @PostMapping("/pending")
    public PageList<SearchMasterMsg> pending(@RequestBody SearchMasterMsgQuery query, HttpServletRequest request){
        try {
            return searchMasterMsgService.pending(query, LoginContext.currentLoginInfo(request).getId());
        } catch (Exception e) {
            e.printStackTrace();
            return new PageList<SearchMasterMsg>();
        }

    }

    // 查询已处理消息
    @PostMapping("/processed")
    public PageList<SearchMasterMsg> processed(@RequestBody SearchMasterMsgQuery query, HttpServletRequest request) {
        try {
            return searchMasterMsgService.processed(query, LoginContext.currentLoginInfo(request).getId());
        } catch (Exception e) {
            e.printStackTrace();
            return new PageList<SearchMasterMsg>();
        }

    }

    // 查询用户自己的消息
    @PostMapping("/user")
    public PageList<SearchMasterMsg> user(@RequestBody SearchMasterMsgQuery query, HttpServletRequest request) {
        try {
            return searchMasterMsgService.queryUserMsg(query, LoginContext.currentLoginInfo(request).getId());
        } catch (Exception e) {
            e.printStackTrace();
            return new PageList<SearchMasterMsg>();
        }

    }

    // 处理寻主消息
    @PutMapping("/handle")
    public AjaxResult handle(@RequestBody Pet pet, HttpServletRequest request) {
        try {
            searchMasterMsgService.handle(pet, LoginContext.currentLoginInfo(request).getId());
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("系统繁忙，请稍后重试！");
        }

    }
}
